import 'package:flui/flui.dart';
import 'package:flutter/material.dart';
import 'package:fzcgx_app/network/http.dart';
import 'package:fzcgx_app/ui/base/base_view_model.dart';
import 'package:fzcgx_app/ui/my/worker/model/worker_base_info_model.dart';
import 'package:fzcgx_app/ui/widget/bottom_sheet_widget.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:date_format/date_format.dart';

class WorkerBaseInfoViewModel extends BaseViewModel {
  WorkerBaseInfoModel model;
  TextEditingController nameController = TextEditingController();
  TextEditingController tizhongController = TextEditingController();
  TextEditingController jiguanController = TextEditingController();
  TextEditingController zhuanyeController = TextEditingController();
  TextEditingController minzuController = TextEditingController();
  TextEditingController gongzuonianxianController = TextEditingController();
  TextEditingController zhengzhimianmaoController = TextEditingController();
  TextEditingController hukoudizhiController = TextEditingController();
  TextEditingController xianzhudizhiController = TextEditingController();
  TextEditingController jinjilianxirenController = TextEditingController();
  TextEditingController jinjilianxirendianhuaController =
      TextEditingController();
  TextEditingController chushengnianyueController = TextEditingController();

  WorkerBaseInfoViewModel() {
    this.setState(ViewState.Loading);
  }
  void requestApi() {
    Http.instance.post<WorkerBaseInfoModel>(
        'app/call/getusers_baseinfo_gr', null, onSuccess: (data) {
      if (data != null) {
        model = data;
        nameController.text = model.name;
        tizhongController.text = model.tizhong;
        jiguanController.text = model.jiguan;
        minzuController.text = model.minzu;
        gongzuonianxianController.text = model.gongzuonianxian;
        zhengzhimianmaoController.text = model.zhengzhimianmao;
        hukoudizhiController.text = model.hukoudizhi;
        xianzhudizhiController.text = model.xianzhudizhi;
        jinjilianxirenController.text = model.jinjilianxiren;
        jinjilianxirendianhuaController.text = model.jinjilianxirendianhua;
        chushengnianyueController.text = model.chushengnianyue;
        zhuanyeController.text = model.zhuanye;
        this.setState(ViewState.Success);
      } else {
        this.setState(ViewState.Empty);
      }
    }, onError: (error) {
      this.setState(ViewState.Error);
    }, onFail: (fail) {
      this.setState(ViewState.Error);
    });
  }

  void onPostApi(BuildContext context) {
    if (nameController.text.toString().isEmpty) {
      FLToast.showText(text: '请输入您的姓名');
      return;
    }
    if (model.sex.toString().isEmpty) {
      FLToast.showText(text: '请选择您的性别');
      return;
    }
    if (zhuanyeController.text.toString().isEmpty) {
      FLToast.showText(text: '请输入您的专业');
      return;
    }
    if (jiguanController.text.toString().isEmpty) {
      FLToast.showText(text: '请输入您的籍贯');
      return;
    }
    if (tizhongController.text.toString().isEmpty) {
      FLToast.showText(text: '请输入您的体重');
      return;
    }
    if (minzuController.text.toString().isEmpty) {
      FLToast.showText(text: '请输入您的民族');
      return;
    }
    if (gongzuonianxianController.text.toString().isEmpty) {
      FLToast.showText(text: '请输入您的工作年限');
      return;
    }
    if (zhengzhimianmaoController.text.toString().isEmpty) {
      FLToast.showText(text: '请输入您的政治面貌');
      return;
    }
    if (hukoudizhiController.text.toString().isEmpty) {
      FLToast.showText(text: '请输入您的户口地址');
      return;
    }
    if (xianzhudizhiController.text.toString().isEmpty) {
      FLToast.showText(text: '请输入您的现住地址');
      return;
    }
    if (jinjilianxirenController.text.toString().isEmpty) {
      FLToast.showText(text: '请输入您的紧急联系人');
      return;
    }
    if (jinjilianxirendianhuaController.text.toString().isEmpty) {
      FLToast.showText(text: '请输入您的紧急联系人电话');
      return;
    }
    if (model.maritalStatusStr.toString().isEmpty) {
      FLToast.showText(text: '请选择您的婚姻状态');
      return;
    }
    if (model.chushengnianyue.toString().isEmpty) {
      FLToast.showText(text: '请选择您的出生年月');
      return;
    }
    var dismiss = FLToast.loading(text: '正在保存...');
    Http.instance.post('app/call/setusers_baseinfo_gr', {
      'name': model.name,
      'sex': model.sex,
      'zhuanye': model.zhuanye,
      'jiguan': model.jiguan,
      'tizhong': model.tizhong,
      'minzu': model.minzu,
      'gongzuonianxian': model.gongzuonianxian,
      'zhengzhimianmao': model.zhengzhimianmao,
      'hukoudizhi': model.hukoudizhi,
      // 'visible': model.visible,
      // 'time': model.time,
      'xianzhudizhi': model.xianzhudizhi,
      'jinjilianxiren': model.jinjilianxiren,
      'jinjilianxirendianhua': model.jinjilianxirendianhua,
      'hunyinzhuangtai': model.hunyinzhuangtai,
      'chushengnianyue': model.chushengnianyue,
    }, onSuccess: (data) {
      FLToast.showSuccess(text: '保存成功');
      dismiss();
      Navigator.pop(context);
    }, onError: (error) {
      dismiss();
    }, onFail: (fail) {
      dismiss();
    });
  }

  void onShowBottomSheet(BuildContext context) {
    showDialog(
        barrierDismissible: true, //是否点击空白区域关闭对话框,默认为true，可以关闭
        context: context,
        builder: (BuildContext context) {
          var list = List();
          list.add('男');
          list.add('女');

          return BottomSheetWidget(
            list: list,
            onItemClickListener: (index) async {
              print(list[index]);
              model.sex = list[index].toString() == '男' ? '1' : '2';
              notifyListeners();
              Navigator.pop(context);
            },
          );
        });
  }

  void onShowBottomMaritalStatusSheet(BuildContext context) {
    showDialog(
        barrierDismissible: true, //是否点击空白区域关闭对话框,默认为true，可以关闭
        context: context,
        builder: (BuildContext context) {
          var list = List();
          list.add('单身');
          list.add('结婚');

          return BottomSheetWidget(
            list: list,
            onItemClickListener: (index) async {
              print(list[index]);
              model.hunyinzhuangtai =
                  list[index].toString() == '单身' ? '1' : '2';
              notifyListeners();
              Navigator.pop(context);
            },
          );
        });
  }

  void onShowBottomBirthDateSheet(BuildContext context) {
    DatePicker.showDatePicker(context,
        // 是否展示顶部操作按钮
        showTitleActions: true,
        // 最小时间
        minTime: DateTime(1900, 1, 1),
        // 最大时间
        maxTime: DateTime(2099, 12, 31),
        // change事件
        onChanged: (date) {
        print('change $date');
      },
        // 确定事件
        onConfirm: (date) {
          print('confirm $date');
          model.chushengnianyue = formatDate(date, [
            yyyy,
            "-",
            mm,
            "-",
            dd
          ]);
          notifyListeners();
        },
        // 当前时间
        // currentTime: DateTime.now(),
        // 语言
        locale: LocaleType.zh
    );


  }
}
